﻿package org.xhs.report.common.ui.panel
{
    import __AS3__.vec.*;
    import baidu.lib.utils.*;
    import baidu.ui.controls.*;
    import baidu.ui.managers.*;
    import baidu.wenku.common.*;
    import baidu.wenku.common.model.*;
    import baidu.wenku.common.ui.*;
    import baidu.wenku.common.utils.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.system.*;
    import flash.text.*;
    import flash.utils.*;

    public class SharePanel extends Sprite implements IPanel
    {
        protected var hasDestroyed:Boolean = true;
        protected var closeBtn:Button;
        protected var shareEmbedCodeTextField:TextField;
        protected var sharecopyBtn:Button;
        protected var sharecopyTips:TextField;
        protected var shareMRadioBtn:WenkuRadioButton;
        protected var shareLRadioBtn:WenkuRadioButton;
        protected var screenModel:ScreenModel;
        protected var loaderInfoVo:LoaderInfoVO;
        protected var docTitle:String;
        protected var copyTimer:Timer;
        protected var shmw:Number;
        protected var shmh:Number;
        protected var shlw:Number;
        protected var shlh:Number;
        protected var iconArray:Vector.<Sprite>;
        protected var shareUrl:String = "http://share.baidu.com/s";
        protected const PANEL_WIDTH:Number = 442;
        protected const PANEL_HEIGHT:Number = 341;
        protected const HEADER_HEIGHT:Number = 37;
        protected const SHARE_SIZE:Array;
        protected const SHARE_ID:Array;
        protected const TIPS_SHARE:Array;
        public static const CLOSE_CLICK:String = "close_click";

        public function SharePanel()
        {
            this.SHARE_SIZE = [[[450, 500], [630, 500]], [[450, 376], [630, 503]]];
            this.SHARE_ID = ["renren", "qzone", "tsina", "tqq", "kaixin001", "douban", "hi"];
            this.TIPS_SHARE = ["人人网", "QQ空间", "新浪微博", "腾讯微博", "开心网", "豆瓣网", "百度空间"];
            this.initModel();
            return;
        }// end function

        protected function initModel() : void
        {
            this.screenModel = InstanceManager.createSingletonInstance(ScreenModel);
            this.screenModel.addEventListener(Event.RESIZE, this.screenResize);
            this.loaderInfoVo = InstanceManager.createSingletonInstance(LoaderInfoVO);
            try
            {
                this.docTitle = decodeURIComponent(this.loaderInfoVo.docTitle);
            }
            catch (e:Error)
            {
                Logger.error(e);
                docTitle = loaderInfoVo.docTitle;
            }
            return;
        }// end function

        protected function screenResize(event:Event) : void
        {
            x = (this.screenModel.stageWidth - width) / 2;
            y = (this.screenModel.stageHeight - height) / 2;
            return;
        }// end function

        protected function siClick(event:MouseEvent) : void
        {
            var _loc_2:* = (event.target as Sprite).name;
            var _loc_3:String = "";
            var _loc_4:* = this.loaderInfoVo.domainUrl + "view/" + this.loaderInfoVo.docId + ".html";
            var _loc_5:* = "《" + this.docTitle + "》_百度文库";
            var _loc_6:String = "很不错，推荐大家看看：）";
            WenkuLog.addLog(WenkuLog.LOGURL_SHAREICON + _loc_2, "", 2);
            _loc_3 = this.shareUrl + "?type=text&uid=10002&to=" + _loc_2 + "&url=" + encodeURIComponent(_loc_4) + "&title=" + encodeURIComponent(_loc_5) + "&comment=" + encodeURIComponent(_loc_6);
            if (_loc_2 == "tsina")
            {
                _loc_3 = _loc_3 + "&relateUid=1736315190";
            }
            BrowserUtil.openNewWindow(_loc_3);
            return;
        }// end function

        protected function sharecopyBtnClick(event:MouseEvent) : void
        {
            WenkuLog.addLog(WenkuLog.LOGURL_COPY, "", 2);
            this.copyTimer.reset();
            this.copyTimer.start();
            this.sharecopyTips.visible = true;
            System.setClipboard(this.shareEmbedCodeTextField.text);
            return;
        }// end function

        protected function hideCopyTips(event:TimerEvent) : void
        {
            this.copyTimer.reset();
            if (this.sharecopyTips)
            {
                this.sharecopyTips.visible = false;
            }
            return;
        }// end function

        protected function closeBtnClick(event:MouseEvent) : void
        {
            dispatchEvent(new Event(SharePanel.CLOSE_CLICK));
            return;
        }// end function

        protected function shareRadioClick(event:Event) : void
        {
            var _loc_2:* = this.shareLRadioBtn.selected ? (this.shlw) : (this.shmw);
            var _loc_3:* = this.shareLRadioBtn.selected ? (this.shlh) : (this.shmh);
            var _loc_4:* = this.shareEmbedCodeTextField.text;
            _loc_4 = this.shareEmbedCodeTextField.text.replace(/width=''(.)+?''""width='(.)+?'/g, "width=\'" + _loc_2 + "\'");
            _loc_4 = _loc_4.replace(/height=''(.)+?''""height='(.)+?'/g, "height=\'" + _loc_3 + "\'");
            this.shareEmbedCodeTextField.text = _loc_4;
            return;
        }// end function

        public function resetPanel() : void
        {
            var _loc_16:Sprite = null;
            if (!this.hasDestroyed)
            {
                return;
            }
            this.screenModel.addStageMask();
            this.copyTimer = new Timer(3000, 1);
            this.copyTimer.addEventListener(TimerEvent.TIMER, this.hideCopyTips);
            this.iconArray = new Vector.<Sprite>;
            if (this.loaderInfoVo.isPPT)
            {
                this.shmw = this.SHARE_SIZE[1][0][0];
                this.shmh = this.SHARE_SIZE[1][0][1];
                this.shlw = this.SHARE_SIZE[1][1][0];
                this.shlh = this.SHARE_SIZE[1][1][1];
            }
            else
            {
                this.shmw = this.SHARE_SIZE[0][0][0];
                this.shmh = this.SHARE_SIZE[0][0][1];
                this.shlw = this.SHARE_SIZE[0][1][0];
                this.shlh = this.SHARE_SIZE[0][1][1];
            }
            var _loc_1:* = graphics;
            var _loc_2:* = GradientType.LINEAR;
            var _loc_3:* = new Matrix();
            _loc_3.createGradientBox(this.PANEL_WIDTH, this.HEADER_HEIGHT, Math.PI / 2);
            _loc_1.clear();
            _loc_1.beginGradientFill(_loc_2, [15397616, 14543078], [100, 100], [0, 255], _loc_3);
            _loc_1.drawRect(0, 0, this.PANEL_WIDTH, this.HEADER_HEIGHT);
            _loc_1.endFill();
            _loc_1.beginFill(16777215);
            _loc_1.drawRect(0, this.HEADER_HEIGHT, this.PANEL_WIDTH, this.PANEL_HEIGHT - this.HEADER_HEIGHT);
            _loc_1.endFill();
            _loc_1.lineStyle(1, Config.BORDER_COLOR);
            _loc_1.drawRect(0, 0, this.PANEL_WIDTH, this.PANEL_HEIGHT);
            _loc_1.endFill();
            _loc_1.lineStyle(1, Config.BORDER_COLOR);
            _loc_1.beginFill(16120826);
            _loc_1.drawRoundRect(35, 140, 380, 112, 10);
            _loc_1.endFill();
            var _loc_4:* = new DotLine() as Sprite;
            addChild(_loc_4);
            _loc_4.x = (this.PANEL_WIDTH - _loc_4.width) / 2;
            _loc_4.y = 100;
            var _loc_5:* = new DotLine() as Sprite;
            addChild(_loc_5);
            _loc_5.x = (this.PANEL_WIDTH - _loc_5.width) / 2;
            _loc_5.y = this.PANEL_HEIGHT - 52;
            var _loc_6:* = new TextField();
            var _loc_7:* = new TextFormat("宋体", 12, 3756145, true);
            _loc_6.selectable = false;
            _loc_6.autoSize = "left";
            _loc_6.text = "分享文档";
            _loc_6.setTextFormat(_loc_7);
            addChild(_loc_6);
            _loc_6.x = 5;
            _loc_6.y = (this.HEADER_HEIGHT - _loc_6.height) / 2;
            var _loc_8:* = new TextFormat("宋体", 12, 3881787);
            var _loc_9:* = new TextField();
            new TextField().selectable = false;
            _loc_9.autoSize = "left";
            _loc_9.text = "分享给朋友们：";
            _loc_9.setTextFormat(_loc_8);
            addChild(_loc_9);
            _loc_9.x = 28;
            _loc_9.y = this.HEADER_HEIGHT + 22;
            var _loc_10:* = new TextField();
            new TextField().selectable = false;
            _loc_10.autoSize = "left";
            _loc_10.text = "嵌入播放器";
            _loc_10.setTextFormat(_loc_8);
            addChild(_loc_10);
            _loc_10.x = 28;
            _loc_10.y = this.HEADER_HEIGHT + 73;
            this.shareEmbedCodeTextField = new TextField();
            this.shareEmbedCodeTextField.width = 370;
            this.shareEmbedCodeTextField.height = 110;
            this.shareEmbedCodeTextField.multiline = true;
            this.shareEmbedCodeTextField.wordWrap = true;
            _loc_8.leading = 6;
            this.shareEmbedCodeTextField.defaultTextFormat = _loc_8;
            var _loc_11:* = Config.docType == Config.DOC_TYPE_TXT ? ("txtreader.swf") : ("apireader.swf");
            var _loc_12:* = "<object classid=\'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\' width=\'" + this.shmw + "\' height=\'" + this.shmh + "\' id=\'bdwk\'>";
            _loc_12 = "<object classid=\'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\' width=\'" + this.shmw + "\' height=\'" + this.shmh + "\' id=\'bdwk\'>" + ("<param name=\'movie\' value=\'" + this.loaderInfoVo.domainUrl + "static/flash/" + _loc_11 + "?docurl=" + this.loaderInfoVo.docUrl + "&cdnurl=" + (this.loaderInfoVo.cdnUrl ? (this.loaderInfoVo.cdnUrl) : (this.loaderInfoVo.docUrl)) + "&docid=" + this.loaderInfoVo.docId + "&catal=" + this.loaderInfoVo.hasCatal + "&doctype=" + this.loaderInfoVo.docType + "&readertype=external&title=" + encodeURIComponent(this.docTitle) + "\'></param><param name=\'wmode\' value=\'window\'></param><param name=\'allowscriptaccess\' value=\'always\'><param name=\'allowfullscreen\' value=\'true\'></param>");
            _loc_12 = _loc_12 + ("<embed src=\'" + this.loaderInfoVo.domainUrl + "static/flash/" + _loc_11 + "?docurl=" + this.loaderInfoVo.docUrl + "&cdnurl=" + (this.loaderInfoVo.cdnUrl ? (this.loaderInfoVo.cdnUrl) : (this.loaderInfoVo.docUrl)) + "&docid=" + this.loaderInfoVo.docId + "&catal=" + this.loaderInfoVo.hasCatal + "&doctype=" + this.loaderInfoVo.docType + "&readertype=external&title=" + encodeURIComponent(this.docTitle) + "\' type=\'application/x-shockwave-flash\' wmode=\'window\' allowscriptaccess=\'always\' allowfullscreen=\'true\' width=\'" + this.shmw + "\' height=\'" + this.shmh + "\' name=\'bdwk\'></embed></object>");
            this.shareEmbedCodeTextField.text = _loc_12;
            addChild(this.shareEmbedCodeTextField);
            this.shareEmbedCodeTextField.x = 40;
            this.shareEmbedCodeTextField.y = 143;
            var _loc_13:* = new AllSkins();
            this.sharecopyBtn = new Button();
            this.sharecopyBtn.setStyle("skin", _loc_13["skin_copy"]);
            this.sharecopyBtn.x = (this.PANEL_WIDTH - 64) / 2;
            this.sharecopyBtn.y = this.PANEL_HEIGHT - 40;
            this.sharecopyBtn.useHandCursor = true;
            addChild(this.sharecopyBtn);
            this.sharecopyBtn.addEventListener(MouseEvent.CLICK, this.sharecopyBtnClick);
            this.sharecopyTips = new TextField();
            this.sharecopyTips.selectable = false;
            this.sharecopyTips.autoSize = "left";
            this.sharecopyTips.text = "嵌入播放器代码复制成功";
            this.sharecopyTips.setTextFormat(_loc_8);
            addChild(this.sharecopyTips);
            this.sharecopyTips.x = this.sharecopyBtn.x + 70;
            this.sharecopyTips.y = this.PANEL_HEIGHT - 32;
            this.sharecopyTips.visible = false;
            this.shareMRadioBtn = new WenkuRadioButton();
            addChild(this.shareMRadioBtn);
            this.shareMRadioBtn.setStyle("textFormat", _loc_8);
            this.shareMRadioBtn.autoSize = "left";
            this.shareMRadioBtn.setPosition(46, this.PANEL_HEIGHT - 86);
            this.shareMRadioBtn.value = "m";
            this.shareMRadioBtn.label = "普通尺寸（" + this.shmw + "*" + this.shmh + "px）";
            this.shareMRadioBtn.groupName = "size";
            this.shareMRadioBtn.selected = true;
            this.shareLRadioBtn = new WenkuRadioButton();
            addChild(this.shareLRadioBtn);
            this.shareLRadioBtn.setStyle("textFormat", _loc_8);
            this.shareLRadioBtn.autoSize = "left";
            this.shareLRadioBtn.setPosition(207, this.PANEL_HEIGHT - 86);
            this.shareLRadioBtn.value = "l";
            this.shareLRadioBtn.label = "较大尺寸（" + this.shlw + "*" + this.shlh + "px）";
            this.shareLRadioBtn.groupName = "size";
            this.shareLRadioBtn.addEventListener(MouseEvent.CLICK, this.shareRadioClick);
            this.shareMRadioBtn.addEventListener(MouseEvent.CLICK, this.shareRadioClick);
            this.closeBtn = new Button();
            this.closeBtn.setStyle("skin", _loc_13["skin_relativeclose"]);
            this.closeBtn.x = this.PANEL_WIDTH - 21;
            this.closeBtn.y = 10;
            this.closeBtn.useHandCursor = true;
            addChild(this.closeBtn);
            this.closeBtn.addEventListener(MouseEvent.CLICK, this.closeBtnClick);
            var _loc_14:int = 0;
            var _loc_15:* = this.SHARE_ID.length;
            while (_loc_14 < _loc_15)
            {
                
                _loc_16 = new (getDefinitionByName("si_" + this.SHARE_ID[_loc_14]) as Class)() as Sprite;
                addChild(_loc_16);
                _loc_16.name = this.SHARE_ID[_loc_14];
                _loc_16.addEventListener(MouseEvent.CLICK, this.siClick);
                TooltipManager.getInstance().register(_loc_16, "分享到" + this.TIPS_SHARE[_loc_14]);
                _loc_16.buttonMode = true;
                _loc_16.x = 118 + _loc_14 * 40;
                _loc_16.y = this.HEADER_HEIGHT + 20;
                this.iconArray.push(_loc_16);
                _loc_14++;
            }
            this.screenModel.stage.addChild(this);
            x = (this.screenModel.stageWidth - this.PANEL_WIDTH) / 2;
            y = (this.screenModel.stageHeight - this.PANEL_HEIGHT) / 2;
            this.hasDestroyed = false;
            return;
        }// end function

        public function destroy() : void
        {
            var _loc_2:DisplayObject = null;
            if (this.hasDestroyed)
            {
                return;
            }
            this.copyTimer.reset();
            this.copyTimer.removeEventListener(TimerEvent.TIMER, this.hideCopyTips);
            this.sharecopyBtn.removeEventListener(MouseEvent.CLICK, this.sharecopyBtnClick);
            this.shareLRadioBtn.removeEventListener(MouseEvent.CLICK, this.shareRadioClick);
            this.shareMRadioBtn.removeEventListener(MouseEvent.CLICK, this.shareRadioClick);
            this.closeBtn.removeEventListener(MouseEvent.CLICK, this.closeBtnClick);
            var _loc_1:int = 0;
            while (_loc_1 < this.iconArray.length)
            {
                
                (this.iconArray[_loc_1] as Sprite).removeEventListener(MouseEvent.CLICK, this.siClick);
                TooltipManager.getInstance().unregister(this.iconArray[_loc_1] as Sprite);
                _loc_1++;
            }
            this.iconArray = null;
            while (this.numChildren > 0)
            {
                
                _loc_2 = getChildAt(0);
                this.removeChildAt(0);
                _loc_2 = null;
            }
            this.screenModel.removeStageMask();
            if (this.screenModel.stage.contains(this))
            {
                this.screenModel.stage.removeChild(this);
            }
            this.hasDestroyed = true;
            return;
        }// end function

    }
}
